
package com.guns.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.guns.Demo;
import com.guns.domain.ActionLog;
import com.guns.domain.Car;
import com.guns.domain.CarInfo;
import com.guns.server.ActionLogServer;
import com.guns.server.CarApiServer;
import com.guns.server.CarMaServer;
import com.guns.server.CarServer;
//import com.guns.utils.R;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.event.ActionEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@RestController
@RequestMapping(value = "/park/interface")
public class CarApiController {
    @Autowired
    private CarApiServer carapiserver;
    @Autowired
    private CarServer carServer;
    @Autowired
    private ActionLogServer actionLogServer;
    @Autowired
    private CarMaServer carMaServer;

    @PostMapping(value = "/exitpass")
    public synchronized int exitPass(@RequestParam(value = "car_number") String carNumber, @RequestParam(value = "io_time") String ioTime,
                                     @RequestParam(value = "channel_id") String channelId,@RequestParam(value="direction")int direction) throws ParseException {
        //外来车辆
        if(carServer.selectByNumber(carNumber)==null){
            addActionLog(carNumber,ioTime,channelId,"入场","异常通行记录","外来车辆异常记录");
            ActionEvent e=new ActionEvent("", 0, "");
            //外来车辆！
            return  4;
        }
        //车辆信息
        CarInfo carInfo=carServer.selectByNumber(carNumber);
        //私家车

        if(carInfo.getStatus()==2){
            addActionLog(carNumber,ioTime,channelId,"","正常通行记录","私家车通行记录");
            //"正常通行"
            return  1;
        }
        //公车
        //派遣单信息
        Car car=carapiserver.selectByNumber(carNumber);
        Date gotime=car.getCometime();
        Date cometime=car.getGotime();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date iotime=sf.parse(ioTime);
        if(iotime.compareTo(gotime)<=0){
            addActionLog(carNumber,ioTime,channelId,"出场","异常通行记录","此公车不到规定出行时间就要出场");
            //不到出行时间！
            return  3;
        }
        else if(iotime.compareTo(cometime)>=0){
            String come=sf.format(cometime);
            carServer.updateStatusByNum(0,carNumber);
            carapiserver.updateByNum(0,carNumber,1);
            carMaServer.updateByNum(1,carNumber);
            addActionLog(carNumber,ioTime,channelId,"入场","异常通行记录","此公车没有在返场时间:"+come+"内返场");
            //改变车辆信息状态和派遣单状态
            //超时回库
            return  2;
        }
        else {
            if(direction==0){
                carServer.updateStatusByNum(1,carNumber);
                carapiserver.updateByNum(1,carNumber,3);
                carMaServer.updateByNum(0,carNumber);
                addActionLog(carNumber,ioTime,channelId,"出场","正常通行记录","");
                return 1;
            }
            else {
                carServer.updateStatusByNum(0, carNumber);
                carapiserver.updateByNum(0, carNumber,1);
                carMaServer.updateByNum(1,carNumber);
                addActionLog(carNumber, ioTime, channelId, "入场", "正常通行记录", "");
                return 1;
            }
        }



    }
    //日志实体类初始化
    public void addActionLog(String carNumber,String ioTime,String channelId,String direction,String logType,String remark){
        ActionLog actionLog=new ActionLog();
        actionLog.setCarNumber(carNumber);
        actionLog.setIoTime(ioTime);
        actionLog.setChannelId(channelId);
        actionLog.setDirection(direction);
        actionLog.setLogType(logType);
        actionLog.setRemark(remark);
        actionLogServer.addActionLog(actionLog);
    }
}

